package com.zzxz.practicaltrainingsys.controller;

import com.zzxz.practicaltrainingsys.entity.*;
import com.zzxz.practicaltrainingsys.service.SchoolService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/school")
@CrossOrigin
public class SchoolController {
    @Autowired
    private SchoolService schoolService;

    @GetMapping("/list")
    public List<School> getSchoolList(){
        return schoolService.getSchool();
    }

    // 新增学校接口
    @PostMapping("/insert")
    public int insertSchool(@RequestBody School school) {
        if (school.getSchool_name() == null || school.getSchool_name().trim().isEmpty()) {
            return -1;
        }
        // 校验名称是否重复（可选）
        List<School> all = schoolService.getSchool();
        for (School s : all) {
            if (s.getSchool_name().equals(school.getSchool_name())) {
                return 0;
            }
        }
        boolean success = schoolService.addSchool(school);
        return success ? 1 : -2;
    }

    //更新
    @PostMapping("/update")
    public int updateSchool(@RequestBody School school){
        int result = schoolService.updateSchool(school);
        return result;
    }
}